package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.dto.RelationAttrInfoDTO;
import com.macro.mall.tiny.modules.pms.model.PmsProductAttribute;
import com.macro.mall.tiny.modules.pms.model.PmsProductCategory;
import com.macro.mall.tiny.modules.pms.service.PmsProductAttributeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-08-15
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @Autowired
    PmsProductAttributeService productAttributeService;

    @ApiOperation("获取分类的所有属性")
    @RequestMapping(value = "/attrInfo/{pcId}")
    public CommonResult<List<RelationAttrInfoDTO>> getAttrInfo(@PathVariable Long pcId) {
        return CommonResult.success(productAttributeService.getRelationAttrInfoByPcId(pcId));
    }

    @ApiOperation("分页查询分类的属性")
    @RequestMapping(value = "/list/{pacId}", method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductAttribute>> list(@PathVariable Long pacId,
                                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "type", defaultValue = "0") Integer type) {
        Page<PmsProductAttribute> page = productAttributeService.list(pacId, pageSize, pageNum, type);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("根据商品属性id获取商品属性")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<PmsProductAttribute> getItem(@PathVariable Long id) {
        PmsProductAttribute productAttribute = productAttributeService.getById(id);
        return CommonResult.success(productAttribute);
    }

    @ApiOperation("更新商品属性")
    @RequestMapping(value = "update/{id}", method = RequestMethod.POST)
    public CommonResult update(@RequestBody PmsProductAttribute productAttribute) {
        boolean success = productAttributeService.updateById(productAttribute);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("添加商品属性")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public CommonResult create(@RequestBody PmsProductAttribute productAttribute) {
        boolean success = productAttributeService.create(productAttribute);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除商品属性")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestParam(value = "ids") List<Long> ids) {
        boolean success = productAttributeService.delete(ids);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}

